package com.qj.yantaishan.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qj.yantaishan.common.core.domain.R;
import com.qj.yantaishan.common.utils.StringUtils;
import com.qj.yantaishan.domain.StoreNews;
import com.qj.yantaishan.service.IStoreNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺新闻表(ShanglalaStoreNews)表控制层
 *
 * @author makejava
 * @since 2022-09-28 10:31:33
 */
@RestController
@RequestMapping("/yantaishan/StoreNews")
public class StoreNewsController {
    /**
     * 服务对象
     */
    @Autowired
    private IStoreNewsService storeNewsService;

    @GetMapping("list")
    public R getStoreStoreNewsList(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                   @RequestParam(required = false, defaultValue = "") String keyword,
                                   @RequestParam(required = false, defaultValue = "") Integer queryType,
                                   @RequestParam(required = false, defaultValue = "") Integer categoryId) {
        Page<StoreNews> storeNewsCategoryPage = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<StoreNews> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(categoryId)) {
            wrapper.eq(StoreNews::getCatId, categoryId);
        }
        if (StringUtils.isNotEmpty(keyword)) {

            if (StringUtils.isNotNull(queryType)) {

                if (queryType == 1) {
                    wrapper.like(StoreNews::getContent, keyword);
                }
                if (queryType == 2) {
                    wrapper.like(StoreNews::getAuthor, keyword);
                }

            }

        }
        final Page<StoreNews> page = storeNewsService.page(storeNewsCategoryPage, wrapper);
        return R.ok(page);
    }

    @GetMapping
    public R getStoreNewsById(Integer newsId) {
        return R.ok(storeNewsService.getById(newsId));
    }

    @PostMapping
    public R add(@RequestBody StoreNews storeNews) {
        storeNews.setAddTime(Math.toIntExact(System.currentTimeMillis() / 1000));
        storeNews.setUpdateTime(Math.toIntExact(System.currentTimeMillis()/1000));
        return R.ok(storeNewsService.save(storeNews));
    }

    @PutMapping
    public R update(@RequestBody StoreNews storeNews) {
        storeNews.setUpdateTime(Math.toIntExact(System.currentTimeMillis()/1000));
        return R.ok(storeNewsService.updateById(storeNews));
    }

    @DeleteMapping
    public R delete(@RequestParam(required = true) Integer newsId) {
        return R.ok(storeNewsService.removeById(newsId));
    }

    @RequestMapping("/news")
    public String getNew(){
        List<StoreNews> list = storeNewsService.selectLastThr();
        return JSON.toJSONString(list);
    }

}

